package com.hainatong.shiphelper.module.warehouse.fragment;

import android.view.View;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentLinyongChukuBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.fragment.CommonResultFragment;
import com.hainatong.shiphelper.module.warehouse.entity.ChuKuRaramBean;
import com.hainatong.shiphelper.module.warehouse.entity.ChukuGoodsParam;
import com.hainatong.shiphelper.module.warehouse.entity.OrgUser;
import com.hainatong.shiphelper.module.warehouse.entity.ShipOrgBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.LinYongViewModel;
import com.hainatong.shiphelper.module.workermgr.entity.WorkerBean;
import com.hainatong.shiphelper.module.workermgr.fragment.WorkerFragment;
import com.hainatong.shiphelper.utils.ComDataUtils;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.shiphelper.widget.PickerDialog;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

/**
 *  出库  选择领用人 页面
 */
public class LinYongFragment extends MvvmLazyFragment<FragmentLinyongChukuBinding, LinYongViewModel>
{
    public static String CHUKU_GOODS_PARAMS = "chukuGoodsBean";
    public static int CHUKU_PARAMS_CODE = 0x82;

    public static String FINISH_CONFIRM_CHUKU = "chukuSuccess";

    private PickerDialog pickerDialog;
    private int selectTag= -1;

    private List<String> shipList;
    private List<String> levelList;
    private List<String> jobList;
    private List<String> workerList;
    private List<OrgUser> orgUserList;

    private List<ChukuGoodsParam> goodsParamsList;
    private LoadingDialog loading;
    private List<ShipOrgBean> shipOrgList;

    private OrgUser selectOrgUser;
    private String selectShipName;
    private WorkerBean linYongWorker;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_linyong_chuku;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        shipList = new ArrayList<>();

        loading = new LoadingDialog( getActivity(), getString( R.string.loading ) );

        pickerDialog = new PickerDialog(getActivity(), (selectStr, position) -> {
            if( selectTag == 1 )
            {
                selectShipName = selectStr;
                //选船
                viewDataBinding.tvShip.setText( selectStr );
            }
            else if ( selectTag == 2 )
            {
                // 操作 级别 列表
                viewDataBinding.tvLevel.setText( selectStr );
                // 选完 level  取出 职能列表
                jobList = LinYongViewModel.getJobListOrg( shipOrgList , selectStr );

                orgUserList = LinYongViewModel.getOrgUser(shipOrgList , selectStr );

                // 去掉
                /*
                if( ObjectUtils.isNotEmpty( orgUserList ) )
                {
                    workerList = LinYongViewModel.getOrgUserNameList( orgUserList );
                }
                */
            }
            else if ( selectTag == 3 )
            {
                //职位
                viewDataBinding.tvJob.setText( selectStr );
                selectOrgUser = orgUserList.get(position);
            }
            /*
            else if ( selectTag == 4 )
            {
                // 领用人
                viewDataBinding.tvLinyongWorkerName.setText( selectStr );
            }
            */
            ToastUtils.showShort( selectStr );
        });

        // 本人 船舶
        String shipname = GlobalConfig.getInstance().getUserInfo().getShip_name();

        shipList.add(shipname);
        // 选船
        viewDataBinding.btnShipSelect.setOnClickListener( v -> {
            selectTag = 1;
            pickerDialog.showPickContent(shipList );
        } );

        //选部门
        viewDataBinding.btnLevelSelect.setOnClickListener( v -> {
            if(ObjectUtils.isNotEmpty( levelList ))
            {
                selectTag = 2;
                pickerDialog.showPickContent(levelList );
            }
        } );

        //选职位
        viewDataBinding.btnJobSelect.setOnClickListener( v -> {
            if( ObjectUtils.isNotEmpty( jobList ) )
            {
                selectTag = 3;
                pickerDialog.showPickContent(jobList );
            }
            else
            {
                ToastUtils.showShort( "先选择级别" );
            }
        } );

        // 跳转到 选择 员工 页面
        viewDataBinding.btnLinyongWorkerSelect.setOnClickListener( v -> {
            // 选择完 职能 后
            if( ObjectUtils.isNotEmpty( selectOrgUser ) )
            {
               // selectTag = 4;
                //pickerDialog.showPickContent(workerList);

                LiveEventBus.get(WorkerFragment.SELECT_WORKER_TAG , Boolean.class)
                        .post( true );
                ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_WORKER_SELECT_VIEW, GsonUtil.obj2json(selectOrgUser)   );
            }
            else
            {
                ToastUtils.showShort( "先选择职位" );
            }
        } );

        // 提交 出库
        viewDataBinding.btnConfirmChuku.setOnClickListener( v -> {

            String remarkStr = viewDataBinding.etChukuInfo.getText().toString();

            if(goodsParamsList != null && !goodsParamsList.isEmpty() && ObjectUtils.isNotEmpty( linYongWorker ) && !StringUtils.isEmpty(selectShipName))
            {
                // 领用人
               String worker =  linYongWorker.getUserTrueName();
               String workerId = linYongWorker.getUuid();
               //操作人
                String charge = GlobalConfig.getInstance().getUserInfo().getUserTrueName();
                String chargeId = GlobalConfig.getInstance().getUserInfo().getUuid();

                ChuKuRaramBean[] chuKuBeans = ComDataUtils.getChukuParamsByLinYongInfo( goodsParamsList,worker,workerId,remarkStr,charge,chargeId , selectShipName);
                loading.show();
                viewModel.chukuTask( chuKuBeans );
            }
        } );

        // 出库 添加的 商品
        LiveEventBus.get(CHUKU_GOODS_PARAMS, RxBusBaseMessage.class)
                    .observeSticky(this,rxMsg -> {
                        if( rxMsg.getCode() == CHUKU_PARAMS_CODE )
                        {
                            goodsParamsList = (List<ChukuGoodsParam>) rxMsg.getObject();
                        }
                    });

        LiveEventBus.get(WorkerFragment.SELECT_WORKER_TAG, WorkerBean.class)
                    .observeSticky( this , workerBean -> {
                        // 拿到 选择的 领用人
                        if( ObjectUtils.isNotEmpty( workerBean ) )
                        {
                            //领用人
                            viewDataBinding.tvLinyongWorkerName.setText( workerBean.getUserTrueName() );
                            linYongWorker = workerBean;
                        }
                    } );

    }


    @Override
    protected void lazyLoad()
    {
        // 先 查询 人员组织 架构
        viewModel.queryOrgUserListByLan("zh");
        // 拿到  组织架构  列表
        viewModel.getOrgUserResult().observe( this , result -> {

                if( result.isSuccess() )
                {
                   shipOrgList = result.getData();
                    // 获取到 级别  列表（ 管理 操作 等 ）
                   levelList = LinYongViewModel.getDepartListInOrg( result.getData() );

                }
                else
                {
                    ToastUtils.showShort( result.getMsg() );
                }
        } );


        //提交 完 出库 任务
        viewModel.getChukuResult().observe(this,result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                LiveEventBus.get(FINISH_CONFIRM_CHUKU,RxBusBaseMessage.class)
                            .post( new RxBusBaseMessage(CommonResultFragment.CHUKU_SUCCESS_RESULT,result.getData()) );
                getActivity().finish();
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        });
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        loading.dismiss();
    }
}
